package com.shundr.shipper.frame.constant;

import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ComponentName;
import android.content.Context;
import android.database.Cursor;
import android.provider.CallLog.Calls;
import android.telephony.TelephonyManager;

import com.shundr.shipper.common.model.AreaInfo;
import com.shundr.shipper.common.util.PrefConstants;
import com.shundr.shipper.common.util.PrefUtils;
import com.shundr.shipper.common.util.ToastHelper;
import com.shundr.shipper.common.util.TtsTools;
import com.shundr.shipper.frame.util.LLogger;
import com.shundr.shipper.frame.util.StringUtil;
import com.shundr.shipper.truck.model.TruckInfo;
import com.shundr.shipper.user.dao.UserDao;
import com.shundr.shipper.user.model.UserInfo;

/**
 * 全局变量类
 * LContext
 * 
 * @author larry 2015-5-25 下午1:30:32 
 * @version 1.0.0
 *
 */
public class LContext {

	/*********** 全局变量 **************/

	public static final int PAGE_SIZE = 20;
	public static final String nullStr = "null";

	public static Context context;
	public static String ACCESS_TOKEN = "";// 登录用户的验证机制
	public static String ADMIN_TOKEN = "";// 登录用户的管理员验证机制
	public static int USER_ID = 0;// 登录用户的验证机制

	public static int width;
	public static int height;

	public static AreaInfo areaInfo;// 当前位置
	public static UserInfo userInfo;// 当前登录用户
	public static String mAddress = "";
	
	public static TtsTools mTtsTools;
	public static boolean isTtsOpened = true;//默认可使用讯飞播报
	
	public static boolean isOrdered = false;
	
	public static boolean isNetworkConnected = false;//网络是否连接
	public static int networkType = 0;//网络类型
	
	/**
	 * 中转用
	 */
	public static TruckInfo truckInfo;//拨打电话的车主对象
	
	public static TtsTools getTtsTools(Context context){
		if(mTtsTools == null){
			mTtsTools = new TtsTools(context);
		}
		return mTtsTools;
	}

	/**
	 * 
	 * getUserInfo(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param ctxt
	 * @return UserInfo
	 * @exception
	 * @since 1.0.0
	 */
	public static UserInfo getUserInfo(Context ctxt) {
		if (StringUtil.isNull(ACCESS_TOKEN)) {
			return null;
		} else {
			if (userInfo == null) {
				return new UserDao().getUserById(USER_ID);
			} else {
				return userInfo;
			}
		}
	}
	
	/**
	 * 货主货主的认证状态
	 * getUserValidationStatus(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @return
	 * int
	 * @exception
	 * @since  1.0.0
	 */
	public static int getUserValidationStatus(){
		int status = -1;
		try {
			UserInfo info = LContext.userInfo;
			if(info == null)
				return -1;
			if(info.getIdValidationStatus() == 2 && info.getCompanyValidationStatus() == 2){
				status = 1;//个人认证和企业认证均已通过
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
			
		return status;
	}
	
	/**
	 * 判断用户是否提交过认证
	 * isCommitValidation(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @return
	 * boolean
	 * @exception
	 * @since  1.0.0
	 */
	public static boolean isCommitValidation(){
		try {
			UserInfo info = LContext.userInfo;
			if(info.getCompanyValidationStatus() == 0){//均未提交认证
				return false;//个人及企业均未提交认证
			}else{
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 判断用户是否已登录 isLogin(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
	 * 
	 * @return boolean
	 * @exception
	 * @since 1.0.0
	 */
	public static boolean isLogin() {
		if (userInfo != null) {
			return true;
		}
		return false;
	}

	/**
	 * 应用是否在后台
	 * isBackground(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param context
	 * @return
	 * boolean
	 * @exception
	 * @since  1.0.0
	 */
	public static boolean isBackground(Context context) {
		ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            ComponentName topActivity = tasks.get(0).topActivity;
            if (!topActivity.getPackageName().equals(context.getPackageName())) {
            	LLogger.e("larry" ,"后台 ---- " + context.getPackageName());
                return true;
            }
        }
        LLogger.e("larry" ,"前台 ---- " + context.getPackageName());
        return false;
	}

	/**
	 * 
	 * init(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param context
	 * @exception
	 * @since 1.0.0
	 */
	public static void init(Context context) {
		if (context == null) {
			return;
		}
		isTtsOpened = PrefUtils.getPrefBoolean(context, PrefConstants.IS_TTS_ON, true);
		mTtsTools = new TtsTools(context);
		LContext.context = context;
	}
	
	/**
	 * sim卡是否良好
	 * isSimExist(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param context
	 * @return
	 * boolean
	 * @exception
	 * @since  1.0.0
	 */
	public static boolean isSimExist(Context context) {
		TelephonyManager mTelephonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		int simState = mTelephonyManager.getSimState();
		boolean flag = true;
		switch (simState) {
		case TelephonyManager.SIM_STATE_ABSENT:// 无卡
			flag = false;
			break;
		case TelephonyManager.SIM_STATE_NETWORK_LOCKED:// 需要NetworkPIN解锁
			break;
		case TelephonyManager.SIM_STATE_PIN_REQUIRED:// 需要PIN解锁
			break;
		case TelephonyManager.SIM_STATE_PUK_REQUIRED:// 需要PUN解锁
			break;
		case TelephonyManager.SIM_STATE_READY:// 良好
			break;
		case TelephonyManager.SIM_STATE_UNKNOWN:// 未知状态
			flag = false;
			break;
		}
		if(!flag){
			ToastHelper.toastShort(context, "请检查手机卡状态后重试");
		}
		return flag;
	} 
	
	/**
	 * 获取拨打电话的时间
	 * getCallLong(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param context
	 * @return
	 * long
	 * @exception
	 * @since  1.0.0
	 */
	public static long getCallLong(Activity context) {
		Cursor cursor = context.getContentResolver().query(Calls.CONTENT_URI,
				new String[] { Calls.DURATION, Calls.TYPE, Calls.DATE }, null,
				null, Calls.DEFAULT_SORT_ORDER);
		context.startManagingCursor(cursor);
		boolean hasRecord = cursor.moveToFirst();
		long incoming = 0L;
		long outgoing = 0L;
		int count = 0;
		if (hasRecord) { // while
			int type = cursor.getInt(cursor.getColumnIndex(Calls.TYPE));
			long duration = cursor.getLong(cursor
					.getColumnIndex(Calls.DURATION));
			switch (type) {
			case Calls.INCOMING_TYPE:
				incoming += duration;
				break;
			case Calls.OUTGOING_TYPE:
				outgoing += duration;
			default:
				break;
			}
			count++;
			// hasRecord = cursor.moveToNext();
		}
		LLogger.e("共计 " + count + "次通话 . 总通话时长 " + (incoming + outgoing)
				+ "秒. 其中接听 " + incoming + " 秒, 拔打 " + outgoing + " 秒.");
		return outgoing;
	}
}
